package genedit.model.graph;

import java.awt.geom.Point2D;
import java.util.Set;

/**
 * TaÄ�ka spoja veze i ostalih grafiÄ�kih elemenata; SadrÅ¾i podatke o svojim
 * ulaznim i izlaznim vezama, kao i referencu na roditeljskog grafiÄ�kog elemenat
 * u kom je definisana.
 * 
 * @author Igic
 * @version 1.1 18/12/13
 */
public class ConnectionPoint extends Point {

	private static final long serialVersionUID = 1L;

	/**
	 * Set veza koji poÄ�inju iz ove taÄ�ke. U opÅ¡tem sluÄ�aju taj broj je jedan.
	 */
	private Set<Link> starting;

	/**
	 * Set veza koji zavrÅ¡avaju u ovoj taÄ�ki. U opÅ¡tem sluÄ�aju taj broj je
	 * jedan.
	 */
	private Set<Link> ending;

	/**
	 * Roditeljski element u kom se nalazi data konekciona taÄ�ka.
	 */
	private GraphElement parent;

	private double resizeFactorY = 0;
	private double resizeFactorX = 0;
	/**
	 * Promenljiva za praÄ‡enje aktivnosti taÄ�ke, odnosno da li je trenutno na
	 * undo/redo (obrisana) ili je aktivna (prikazana u dokumentu).
	 */
	private transient boolean active = true;

	public ConnectionPoint(Point2D position) {
		super(position);
	}
	
	public void findResizeFactor() {
		this.resizeFactorY = (position.getY() - this.getParent().getPosition().getY())/this.getParent().getSize().getHeight();
		this.resizeFactorX = (position.getX() - this.getParent().getPosition().getX())/this.getParent().getSize().getWidth();
	}


	public Set<Link> getStarting() {
		if (starting == null)
			starting = new java.util.HashSet<Link>();
		return starting;
	}

	public java.util.Iterator<Link> getIteratorStarting() {
		if (starting == null)
			starting = new java.util.HashSet<Link>();
		return starting.iterator();
	}

	public void setStarting(java.util.Collection<Link> newStarting) {
		removeAllStarting();
		for (java.util.Iterator<Link> iter = newStarting.iterator(); iter.hasNext();)
			addStarting((Link) iter.next());
	}

	public void addStarting(Link newLink) {
		if (newLink == null)
			return;
		if (this.starting == null)
			this.starting = new java.util.HashSet<Link>();
		if (!this.starting.contains(newLink)) {
			this.starting.add(newLink);

		}
	}

	public void removeStarting(Link oldLink) {
		if (oldLink == null)
			return;
		if (this.starting != null)
			if (this.starting.contains(oldLink)) {
				this.starting.remove(oldLink);
				oldLink.setStart((ConnectionPoint) null);
			}
	}

	public void removeAllStarting() {
		if (starting != null) {
			Link oldLink;
			for (java.util.Iterator<Link> iter = getIteratorStarting(); iter.hasNext();) {
				oldLink = (Link) iter.next();
				iter.remove();
				oldLink.setStart((ConnectionPoint) null);
			}
		}
	}

	public Set<Link> getEnding() {
		if (ending == null)
			ending = new java.util.HashSet<Link>();
		return ending;
	}

	public double getResizeFactorY() {
		return resizeFactorY;
	}

	public void setResizeFactorY(double resizeFactorY) {
		this.resizeFactorY = resizeFactorY;
	}

	public double getResizeFactorX() {
		return resizeFactorX;
	}

	public void setResizeFactorX(double resizeFactorX) {
		this.resizeFactorX = resizeFactorX;
	}

	public java.util.Iterator<Link> getIteratorEnding() {
		if (ending == null)
			ending = new java.util.HashSet<Link>();
		return ending.iterator();
	}

	public void setEnding(java.util.Collection<Link> newEnding) {
		removeAllEnding();
		for (java.util.Iterator<Link> iter = newEnding.iterator(); iter.hasNext();)
			addEnding((Link) iter.next());
	}

	public void addEnding(Link newLink) {
		if (newLink == null)
			return;
		if (this.ending == null)
			this.ending = new java.util.HashSet<Link>();
		if (!this.ending.contains(newLink)) {
			this.ending.add(newLink);
		}
	}

	public void removeEnding(Link oldLink) {
		if (oldLink == null)
			return;
		if (this.ending != null)
			if (this.ending.contains(oldLink)) {
				this.ending.remove(oldLink);
				oldLink.setEnd((ConnectionPoint) null);
			}
	}

	public void removeAllEnding() {
		if (ending != null) {
			Link oldLink;
			for (java.util.Iterator<Link> iter = getIteratorEnding(); iter.hasNext();) {
				oldLink = (Link) iter.next();
				iter.remove();
				oldLink.setEnd((ConnectionPoint) null);
			}
		}
	}

	public GraphElement getParent() {
		return parent;
	}

	public void setParent(GraphElement newGraphElement) {
		this.parent=newGraphElement;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

}